// pages/warn/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 故障选项
    itemsValue: [
      {
        checked: false,
        value: "私锁私用",
        color: "#b9dd08"
      },
      {
        checked: false,
        value: "车牌缺损",
        color: "#b9dd08"
      },
      {
        checked: false,
        value: "轮胎坏了",
        color: "#b9dd08"
      },
      {
        checked: false,
        value: "车锁坏了",
        color: "#b9dd08"
      },
      {
        checked: false,
        value: "违规乱停",
        color: "#b9dd08"
      },
      {
        checked: false,
        value: "密码不对",
        color: "#b9dd08"
      },
      {
        checked: false,
        value: "刹车坏了",
        color: "#b9dd08"
      },
      {
        checked: false,
        value: "其他故障",
        color: "#b9dd08"
      }
    ],
    // 提交按钮的背景色，未勾选类型时无颜色
    btnBgc: "",
    picUrls:[],
    // 故障车编号和备注
    inputValue: {
      num: 0,
      desc: ""
    },
    // 故障类型数组
    checkboxValue: [],
    // 选取图片提示
    actionText: "+",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 勾选故障选项函数
   */
  checkboxChange:function (e) {
    var values = e.detail.value;
    this.setData({
      checkboxValue: values,
    });
    if (values.length == 0 || this.data.picUrls.length == 0){ // 没有勾选故障选项
      this.setData({
        // checkboxValue: values,
        btnBgc: "" // 提交按钮无色
      });
    }else{
      this.setData({
        btnBgc: "#b9dd08"
      });
    }
  },

  /**
   * 上传图片
   */
  bindCamera:function () {
    wx.chooseImage({
      success:(res) => {
        console.log(res.tempFilePaths)
        console.log(res.tempFiles)
        this.setData({
          picUrls: this.data.picUrls.length ? this.data.picUrls.concat(res.tempFilePaths) : res.tempFilePaths,
        })
      },
      complete:() => {
        console.log(22)
        if (this.data.checkboxValue.length != 0 && this.data.picUrls.length != 0) { // 没有勾选故障选项
          console.log(33)
          this.setData({
            btnBgc: "#b9dd08" // 提交按钮无色
          });
        }
      }
    })
    
  },

  /**
   * 删除图片
   */
  delPic:function (e) {
    var index = e.target.dataset.index;
    var _picUrls = this.data.picUrls;
    _picUrls.splice(index, 1);
    this.setData({
      picUrls : _picUrls
    })
    if (this.data.checkboxValue.length == 0 || this.data.picUrls.length == 0) { // 没有勾选故障选项
      this.setData({
        // checkboxValue: values,
        btnBgc: "" // 提交按钮无色
      });
    }
  },


  numberChange:function (e) {
    this.setData({
      inputValue: {
        num: e.detail.value,
        desc: this.data.inputValue.desc
      },
    })
  },
  descChange: function (e) {
    this.setData({
      inputValue: {
        num: this.data.inputValue.num,
        desc: e.detail.value,
      },
    })
  },

  formSubmit:function () {
    this.setData({
      loading:true
    })
    if (this.data.checkboxValue.length != 0 && this.data.picUrls.length != 0){
      wx.request({
        url:"https://www.easy-mock.com/mock/59098d007a878d73716e966f/ofodata/msg",
        data:{
          // picUrls: this.data.picUrls,
          // inputValue: this.data.inputValue,
          // checkboxValue: this.data.checkboxValue
        },
        method:"GET",
        success:() => {
          this.setData({
            loading: false
          })
          wx.showToast({
            title: res.data.data.msg,
            icon: 'success',
            duration: 2000
          })
        }
      })
  }else{
    this.setData({
      loading: false
    })
    wx.showModal({
      title: "请填写反馈信息",
      content: '看什么看，赶快填反馈信息，削你啊',
      confirmText: "我我我填",
      cancelText: "劳资不填",
      success: (res) => {
        if (res.confirm) {
          // 继续填
        } else {
          console.log("back")
          wx.navigateBack({
            delta: 1 // 回退前 delta(默认为1) 页面
          })
        }
      }
    })
  }
  }
})